﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace YKXiangQi.Controls
{
    public class DataGridEx: DataGrid
    {
        public static readonly DependencyProperty RowDoubleClickProperty =
            DependencyProperty.Register("RowDoubleClick", typeof (ICommand), typeof (DataGridEx), new PropertyMetadata(default(ICommand)));

        public ICommand RowDoubleClick
        {
            get { return (ICommand) GetValue(RowDoubleClickProperty); }
            set { SetValue(RowDoubleClickProperty, value); }
        }

        public DataGridEx()
        {
            this.PreviewMouseDoubleClick += OnRowDoubleClick;
        }

        private void OnRowDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if(e.LeftButton == MouseButtonState.Pressed)
            {
                var selectedItem = this.SelectedItem;
                if (selectedItem == null || RowDoubleClick == null) return;

                RowDoubleClick.Execute(selectedItem);
            }
        }
    }
}
